package cn.com.zpark.jdbc;

import cn.com.zpark.jdbc.pojo.User;
import cn.com.zpark.jdbc.utils.JDBCUtils;
import cn.com.zpark.jdbc.utils.JDBCUtils02;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: cn.com.zpark.jdbc
 * @ClassName: JDBC_03_Select
 * @Description:
 * @author: RZS
 * @date: 2021/10/26  17:49
 */
public class JDBC_03_Select {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stat = null;
        ResultSet res = null;

        try {
            // 获取连接
            conn = JDBCUtils02.getConn();
            // 创建statement对象
            stat = conn.createStatement();
            String sql = "select * from user";
            res = stat.executeQuery(sql);

            // 创建集合list，用于存放user对象
            List<User> list = new ArrayList<>();
            // 6、获取查询结果
            while (res.next()) {
                // 创建user对象，存储查询结果
                User user = new User();
                user.setId(res.getInt("id"));
                user.setName(res.getString("name"));
                user.setPwd(res.getString("pwd"));
                user.setMoney(res.getDouble("money"));
                // 将user对象存放的集合中
                list.add(user);

            }
            System.out.println(list);
            for (User user : list) {
                System.out.println(user.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭连接
            JDBCUtils02.close(res, stat, conn);
        }
    }
}
